package cn.itcast.servlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/logoutServlet")
public class LogoutServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //清除页面作用域数据
        request.getSession().invalidate();
        //清除添加的cookie
        Cookie cookie = new Cookie("rememberme", "a");
        cookie.setMaxAge(0);
        cookie.setPath("/");
        response.addCookie(cookie);

        //重定向到登陆界面
        response.sendRedirect(super.getServletContext().getContextPath()+"/index.jsp");
    }
}
